package com.woniu.teachmall04.service;

import com.woniu.teachmall04.entity.Goods;
import com.woniu.teachmall04.mapper.GoodsMapper;
import com.woniu.teachmall04.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MinioService minioService;

    private static final int PAGE_SIZE = 12;
    private static final String GOODS_CACHE_PREFIX = "goods:page:";

    // 获取商品详情
    public Goods getById(Integer id) {
        return goodsMapper.selectById(id);
    }

    // 获取所有商品
    public List<Goods> getAll() {
        return goodsMapper.selectAll();
    }

    // 分页查询商品
    public List<Goods> getByPage(int page, Integer categoryId, String keyword,
                                 BigDecimal minPrice, BigDecimal maxPrice) {
        // 先查缓存
        String cacheKey = buildCacheKey(page, categoryId, keyword, minPrice, maxPrice);
        List<Goods> goodsList = redisService.getCacheGoodsPage(cacheKey);

        if(goodsList == null) {
            int offset = (page - 1) * PAGE_SIZE;
            goodsList = goodsMapper.selectByPage(offset, PAGE_SIZE, categoryId,
                    keyword, minPrice, maxPrice);
            redisService.cacheGoodsPage(cacheKey, goodsList);
        }
        return goodsList;
    }

    // 获取总页数
    public long getTotalPages(Integer categoryId, String keyword,
                              BigDecimal minPrice, BigDecimal maxPrice) {
        long total = goodsMapper.count(categoryId, keyword, minPrice, maxPrice);
        return (total + PAGE_SIZE - 1) / PAGE_SIZE; // 向上取整
    }

    // 获取热门商品
    public List<Goods> getHottest() {
        return goodsMapper.selectHottest();
    }

    // 获取新品
    public List<Goods> getNewest() {
        return goodsMapper.selectNewest();
    }

    // 添加商品
    @Transactional
    public boolean add(Goods goods, MultipartFile imageFile) throws Exception {
        // 上传图片
        if(imageFile != null && !imageFile.isEmpty()) {
            String imageUrl = minioService.uploadFile(imageFile);
            goods.setImage(imageUrl);
        }

        goods.setSaleTime(new Date());
        goods.setStatus("Y");
        int result = goodsMapper.insert(goods);

        if(result > 0) {
            // 清除商品列表缓存
            redisService.deleteAllGoodsPageCache();
            return true;
        }
        return false;
    }

    // 更新商品
    @Transactional
    public boolean update(Goods goods, MultipartFile imageFile) throws Exception {
        // 更新图片
        if(imageFile != null && !imageFile.isEmpty()) {
            String imageUrl = minioService.uploadFile(imageFile);
            goods.setImage(imageUrl);
        }

        int result = goodsMapper.update(goods);
        if(result > 0) {
            // 清除相关缓存
            redisService.deleteAllGoodsPageCache();
            return true;
        }
        return false;
    }

    // 上架/下架商品
    @Transactional
    public boolean toggleStatus(Integer id, String status) {
        int result = goodsMapper.updateStatus(id, status);
        if(result > 0) {
            redisService.deleteAllGoodsPageCache();
            return true;
        }
        return false;
    }

    // 构建缓存键
    private String buildCacheKey(int page, Integer categoryId, String keyword,
                                 BigDecimal minPrice, BigDecimal maxPrice) {
        return GOODS_CACHE_PREFIX + page + ":"
                + (categoryId != null ? categoryId : "all") + ":"
                + (keyword != null ? keyword.hashCode() : "all") + ":"
                + (minPrice != null ? minPrice.toString() : "all") + ":"
                + (maxPrice != null ? maxPrice.toString() : "all");
    }
}